import {
  getList,
  getGroupList,
  getDeviceList,
  getDeviceDetail,
  getPresetList,
  getUserList,
  getCaliDic,
} from 'service/actions/dictionaryList';

export default {
  namespace: 'dictionaryList',
  state: {
    dicList: [],
    groupList: [],
    deviceList: [],
    deviceDetail: {},
    presetList: [],
    userList: [],
    caliList: [],
  },

  effects: {
    // 获取列表数据
    *getList({}, { put, call, select }) {
      const data = yield call(getList);
      if (!data) return
      yield put({
        type: 'updateState',
        payload: { dicList: data.data },
      });
    },

    // 获取部门数据
    *getGroupList({}, { put, call, select }) {
      const data = yield call(getGroupList);
      if (!data) return
      yield put({
        type: 'updateGroupList',
        payload: { groupList: data.data },
      });
    },

    // 获取设备
    *getDeviceList({ payload }, { put, call, select }) {
      const data = yield call(getDeviceList, payload);
      if (data && data.code === '000000') {
        yield put({
          type: 'updateDeviceList',
          payload: { deviceList: data.data },
        });
      }
    },
    // 获取设备数据
    *getDeviceDetail({ payload }, { put, call, select }) {
      const data = yield call(getDeviceDetail, payload);
      if (data && data.code === '000000') {
        yield put({
          type: 'updateDeviceDetail',
          payload: { deviceDetail: data.data },
        });
      }
      return data;
    },
    // 获取预置项目数据
    *getPresetList({ payload }, { put, call, select }) {
      const data = yield call(getPresetList, payload);
      if (data && data.code === '000000') {
        yield put({
          type: 'updatePresetList',
          payload: { presetList: data.data.records },
        });
      }
    },
    // 获取保养员列表
    *getUserList({ payload }, { put, call, select }) {
      const data = yield call(getUserList, payload);
      if (data && data.code === '000000') {
        yield put({
          type: 'updateUsertList',
          payload: { userList: data.data.records },
        });
      }
    },
    // 获取检定字典
    *getCaliList({ payload }, { put, call, select }) {
      const data = yield call(getCaliDic, payload);
      if (data && data.code === '000000') {
        yield put({
          type: 'updateCaliList',
          payload: { caliList: data.data },
        });
      }
    },
  },

  reducers: {
    // 更新List
    updateState(state, { payload: { dicList } }) {
      return { ...state, dicList };
    },
    // 更新groupList
    updateGroupList(state, { payload: { groupList } }) {
      return { ...state, groupList };
    },
    // 更新deviceList
    updateDeviceList(state, { payload: { deviceList } }) {
      return { ...state, deviceList };
    },
    // 更新deviceDetail
    updateDeviceDetail(state, { payload: { deviceDetail } }) {
      return { ...state, deviceDetail };
    },
    // 更新presetList
    updatePresetList(state, { payload: { presetList } }) {
      return { ...state, presetList };
    },
    // 更新userList
    updateUsertList(state, { payload: { userList } }) {
      return { ...state, userList };
    },
    // 更新userList
    updateCaliList(state, { payload: { caliList } }) {
      return { ...state, caliList };
    },
  },
  subscriptions: {},
};
